<?php

class m120503_142232_create_history extends CDbMigration {

	public function safeUp() {
		$history = array(
			'id'=>'pk',
			'translationId'=>'integer',
			'value'=>'text',
			'userId'=>'integer',
			'name'=>'string',
			'createdAt'=>'datetime',
			'updatedAt'=>'datetime',
		);
		$this->createTable('histories', $history);
		$this->createIndex(
			'index_history_translation_id', 'histories', 'translationId'
		);
		$this->createIndex(
			'index_history_updated_at', 'histories', 'updatedAt'
		);

		$auth = Yii::app()->authManager;

		$auth->createOperation('historyUpdate', 'Update History');
		$auth->createOperation('historyDelete', 'Delete History');
		$auth->createOperation('historyManage', 'Manage History');

		$role = $auth->createRole(
			'historyModerator', 'History Moderator'
		);
		$role->addChild('historyUpdate');
		$role->addChild('historyDelete');
		$role->addChild('historyManage');

		$auth->addItemChild('admin', 'historyModerator');
	}

	public function safeDown() {
		$this->dropTable('histories');

		$auth = Yii::app()->authManager;

		$auth->removeItemChild('admin', 'historyModerator');

		$auth->removeItemChild('historyModerator', 'historyUpdate');
		$auth->removeItemChild('historyModerator', 'historyDelete');
		$auth->removeItemChild('historyModerator', 'historyManage');

		$auth->removeAuthItem('historyUpdate');
		$auth->removeAuthItem('historyDelete');
		$auth->removeAuthItem('historyManage');

		$auth->removeAuthItem('historyModerator');
	}

}